var pageIndex = 0;     //页面索引初始值  
var pageSize = 15;     //每页显示条数初始化，修改显示条数，修改这里即可
$(function(){
	$("#Pagination").pagination(getCount(), {  
        callback: PageCallback,  
        prev_text: '上一页',       //上一页按钮里text  
        next_text: '下一页',       //下一页按钮里text  
        items_per_page: pageSize,  //显示条数  
        num_display_entries: 5,    //连续分页主体部分分页条目数  
        current_page: pageIndex,   //当前页索引  
        num_edge_entries: 5        //两侧首尾分页条目数  
    });    

    //翻页调用  
    function PageCallback(index, jq) { 
    	headTable();
        InitTable(index);  
    }  
    
    function getCount(){
    	var count = "";
    	$.ajax({
        	type : 'get',
			dataType: 'text',
			contentType: "application/json",
			async: false, 
            url: "log.do?action=2",      //提交到一般处理程序请求数据  
            success: function(data) { 
            	count = data;
            	if(count == 0){
            		$("#Pagination").hide();
            	}else{
            		$("#Pagination").show();
            		$("#spannum").text(data);
            	}
            }  
         });  
    	
    	return count;
    }
});

//请求数据  
function InitTable(pageIndex) {
    $.ajax({
    	type : 'get',
		dataType: 'json',
		contentType: "application/json",
		async: false, 
        url: 'log.do?action=1&pageIndex=' + (pageIndex + 1) + '&pageSize=' + pageSize,      //提交到一般处理程序请求数据  
        success: function(data) {
        	rowNum = 1;
        	var html = "";
			$.each(data, function(index, value){
				html += getInfoForTable(value.A1,value.logId,value.logTitle,value.logDesc,value.ip,value.exception,value.logType);
			});
			
			$("#tableHeader").after(html);
        }  
    });
}

var rowNum = 1;

function getInfoForTable(a1,logId,logTitle,logDesc,ip,exception,logType){
	var color = "black";
	if(logType == "0"){
		color = "red";
	}
	var type = "";
	if(logType == "0"){
		type ="异常日志";
	}else if(logType == "1"){
		type = "acc操作日志";
	}
	var str = "<tr>";
	str += "<td style=\"color: green;\">"+rowNum+"</td>";
	str += "<td style=\"color: "+color+";\">"+logId+"</td>";
	str += "<td style=\"color: "+color+";\">"+timeFormat(a1)+"</td>";
	str += "<td style=\"color: "+color+";\">"+logTitle+"</td>";
	if(logDesc.length > 50){
		logDesc = logDesc.substring(0, 50);
	}
	str += "<td style=\"color: "+color+";\">"+logDesc+"</td>";
	str += "<td style=\"color: "+color+";\">"+ip+"</td>";
	str += "<td style=\"color: "+color+";\">"+type+"</td>";
	if(exception.length > 30){
		exception = exception.substring(0, 30);
	}
	str += "<td style=\"color: "+color+";\">"+exception+"</td>";
	str += "<td><a style=\"cursor:pointer;\" onclick=\"details('"+logId+"')\">详细</a></td>";
	str += "</tr>";
	rowNum ++;
	return str;
}

function timeFormat(time){
	//201405021018
	var year = time.substring(0, 4);
	var month = time.substring(4,6);
	var day = time.substring(6,8);
	var hour = time.substring(8,10);
	var min = time.substring(10,12);
	return year+"-" + month +"-"+ day +"  "+ hour +":"+ min;
}

function details(vehicleId){
	window.parent.location.href="logdetail.jsp?id="+vehicleId;
}

function dateFormat(date){
	var formatDate = "";
	var month = date.split(' ')[0];
	var day = date.split(' ')[1];
	var year = date.split(' ')[2].replace(",","");
	switch(month){
		case "一月":
			month = "01";
			break;
		case "二月":
			month = "02";
			break;
		case "三月":
			month = "03";
			break;
		case "四月":
			month = "04";
			break;
		case "五月":
			month = "05";
			break;
		case "六月":
			month = "06";
			break;
		case "七月":
			month = "07";
			break;
		case "八月":
			month = "08";
			break;
		case "九月":
			month = "09";
			break;
		case "十月":
			month = "10";
			break;
		case "十一月":
			month = "11";
			break;
		case "十二月":
			month = "12";
			break;
	}
	
	day=day.replace(/,/ig,""); 
	if(day.length == 1){
		day = "0" + day;
	}
	
	formatDate = year + "-" + month + "-" + day;
	return formatDate;
}
function headTable(){
	var string = "";
	string += "<tr id=\"tableHeader\">";
	string += "<td>行号</td>";
	string += "<td>ID</td>";
	string += "<td>时间</td>";
	string += "<td>标题</td>";
	string += "<td>描述</td>";
	string += "<td>IP</td>";
	string += "<td>类型</td>";
	string += "<td>异常</td>";
	string += "<td>操作</td>";
	string += "</tr>";
	
	$("#tableData").html(string);
}

function release(){
	window.parent.location.href = "../release.jsp";
}